package com.fzj.example.design.proxy.proxydynamic.jdk;

import sun.nio.cs.US_ASCII;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test {
    public static void main(String[] args) {

        //方式1
        UserService userService=new UserServiceImpl();
        UserInvoicationHandler  userServiceUserInvoicationHandler=new UserInvoicationHandler(userService);
        UserService proxy = (UserService)userServiceUserInvoicationHandler.getProxy();
        proxy.addUser();

        //方式2

        UserService userService1=new UserServiceImpl();
        UserService proxyInstance =(UserService) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), userService1.getClass().getInterfaces(), new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("Do something before 1");
                Object result = method.invoke(userService1, args);
                System.out.println("Do something after 1");
                return result;
            }
        });

        proxyInstance.deleteUser();

    }
}
